package com.sixmac.service.impl;

import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.CMap;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.grid.BladePage;
import com.sixmac.core.toolbox.support.Convert;
import com.sixmac.model.Collections;
import com.sixmac.model.Comment;
import com.sixmac.model.Content;
import com.sixmac.model.Thubms;
import com.sixmac.module.vo.Tree;
import com.sixmac.service.ContentService;
import org.junit.Test;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/6/9.
 */
@Service
public class ContentServiceImpl implements ContentService {

    public AjaxResult getArticleById(Integer aid, Integer userId) {
        String sql = "select *,(select count(*) from t_thumbs where articleId = c.id and userId =#{userId} limit 0,1 ) as isthumb,(select count(*) from t_collections where articleId = c.id and userId = #{userId} limit 0,1 ) as iscollection from t_content c where id = " + aid;
        if (userId == null || userId == 0) {
            sql = "select *,0 as isthumb,0 as iscollection from t_content c where id = " + aid;
        }

        AjaxResult res = new AjaxResult();
        Map map = Db.selectOne(sql, CMap.init().put("userId", userId));
        if (map == null) {
            return res.addError("文章不存在");
        }
        Content c = new Content();
        c.setId(aid);
        Integer playnum = Convert.toInt(map.get("palynum"));
        if (playnum == null) {
            c.setPalynum(1);
        } else {
            c.setPalynum(playnum + 1);
        }
        Blade.create(Content.class).update(c);
        return res.setData(CMap.init().put("article", map));

    }

    public AjaxResult thumb(Integer userId, Integer aid, Integer type) {
        AjaxResult res = new AjaxResult();
        Content c = Blade.create(Content.class).findById(aid);
        if (type == 1) {
            //生成点赞记录
            Thubms thubms = new Thubms();
            thubms.setUserId(userId);
            thubms.setArticleId(aid);
            Thubms u = Blade.create(Thubms.class).findFirstBy(" articleId =" + aid + " and userId =" + userId, null);
            if (u == null) {
                Boolean b = Blade.create(Thubms.class).save(thubms);
                //增加点赞
                Integer thumbsnum = c.getThumbsnum();
                if (thumbsnum == null) {
                    c.setThumbsnum(1);
                } else {
                    c.setThumbsnum(c.getThumbsnum() + 1);
                }
            } else {
                return res.addError("不要重复点赞");
            }
        } else {
            Integer d = Blade.create(Thubms.class).deleteBy("articleId =#{aid} and userId = #{userId}", CMap.init().put("aid", aid).put("userId", userId));

            if (d > 0) {
                //减少点赞
                Integer thumbsnum = c.getThumbsnum();
                if (thumbsnum == null) {
                    c.setThumbsnum(0);
                } else {
                    c.setThumbsnum(c.getThumbsnum() - 1);
                }
            } else {
                return res.addError("不要重复取消");
            }

        }
        Blade.create(Content.class).update(c);
        return res.addSuccess("操作成功");

    }

    public AjaxResult collection(Integer userId, Integer aid, Integer type) {
        AjaxResult res = new AjaxResult();
        Content c = Blade.create(Content.class).findById(aid);
        if (type == 1) {
            //生成收藏记录
            Collections collections = new Collections();
            collections.setUserId(userId);
            collections.setArticleId(aid);
            Collections u = Blade.create(Collections.class).findFirstBy(" articleId =" + aid + " and userId =" + userId, null);
            if (u == null) {
                Boolean b = Blade.create(Collections.class).save(collections);
                //增加点赞
                Integer collectionnum = c.getCollectionnum();
                if (collectionnum == null) {
                    c.setCollectionnum(1);
                } else {
                    c.setCollectionnum(c.getCollectionnum() + 1);
                }
            } else {
                return res.addError("不要重复收藏");
            }
        } else {
            Integer d = Blade.create(Collections.class).deleteBy("articleId =#{aid} and userId = #{userId}", CMap.init().put("aid", aid).put("userId", userId));

            if (d > 0) {
                //减少点赞
                Integer collectionnum = c.getCollectionnum();
                if (collectionnum == null) {
                    c.setCollectionnum(0);
                } else {
                    c.setCollectionnum(c.getCollectionnum() - 1);
                }
            } else {
                return res.addError("不要重复取消");
            }

        }
        Blade.create(Content.class).update(c);
        return res.addSuccess("操作成功");


    }

    public AjaxResult editComment(Integer userId, Integer aid, Integer pid, String content) {
        AjaxResult res = new AjaxResult();
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setArticleId(aid);
        comment.setContent(content);
        comment.setPid(pid);
        comment.setCreateDate(new Date());
        Content c = Blade.create(Content.class).findById(aid);
        if (c == null) {
            return res.addError("文章id无效");
        }
        if (c.getCommentnum() == null) {
            c.setCommentnum(1);
        } else {
            c.setCommentnum(c.getCommentnum() + 1);
        }
        Blade.create(Content.class).update(c);
        Boolean b = Blade.create(Comment.class).save(comment);


        if (b) {
            res.addSuccess("评论成功");
        } else {
            res.addError("评论失败");
        }
        return res;
    }

    private List getAllCommentByArticle(Integer aid) {
        List maps = Db.selectList("select c.*,u.nickname,u.headimg from t_comment c  left join t_user u on u.id =c.userId where c.articleId= " + aid + " order by createDate desc", null);
        return maps;

    }

    private void getMapById(List<Map> page, List<Map> all, List<Tree> trees) {
        for (Map m : page) {
            Tree t = new Tree();
            t.setMap(m);
            this.getMapById(all, Convert.toInt(m.get("id")), t.getLists());
            trees.add(t);
        }
    }

    private void getMapById(List<Map> maps, Integer id, List<Tree> trees) {
        for (Map m : maps) {
            if (Convert.toInt(m.get("pid")).compareTo(id) == 0) {
                Tree t = new Tree();
                t.setMap(m);
                trees.add(t);
                getMapById(maps, Convert.toInt(m.get("id")), t.getLists());
            }
        }

    }


    public AjaxResult showComment(Integer aid, Integer page, Integer size) {
        AjaxResult res = new AjaxResult();
        List maps = this.getAllCommentByArticle(aid);
        List<Tree> tees = new ArrayList<>();
        BladePage<Map> bladePage = Db.paginate("select c.*,u.nickname,u.headimg from t_comment c  left join t_user u on u.id =c.userId where  c.articleId = #{aid} and c.pid = 0 ", CMap.init().put("aid", aid), page, size);
        this.getMapById(bladePage.getRows(), maps, tees);
        res.setData(tees);
        return res;
    }

    @Test
    public void compare() {
        Integer a = 1234;
        Integer b = 1234;
        System.out.print(a.compareTo(b));
    }


}
